/*
 * Copyright (c) 2013.
 *  游戏服务器核心代码编写人陈磊拥有使用权
 *  联系方式：E-mail:13638363871@163.com ;qq:502959937
 *  个人博客主页：http://my.oschina.net/chenleijava
 */

package com.dc.gameserver.serverCore.controller.handleController.itemsController;

import com.dc.gameserver.proto.itemserverprotoVo.spriteLoadingRequest;
import com.dc.gameserver.proto.itemserverprotoVo.spriteLoadingResponse;
import com.dc.gameserver.proto.itemserverprotoVo.spriteVo;
import com.dc.gameserver.serverCore.controller.AbstractController.AbstractController;
import com.dc.gameserver.serverCore.controller.AbstractController.IController;
import com.dc.gameserver.serverCore.model.dbsql.modelService.PlayerService;
import com.dc.gameserver.serverCore.model.dbsql.modelVo.TSprite;
import com.dc.gameserver.serverCore.service.character.PlayerInstance;
import com.google.protobuf.MessageLite;
import javolution.util.FastTable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author 石头哥哥 </br>
 *         dcserver1.3 </br>
 *         Date:13-12-27 </br>
 *         Time:下午2:34 </br>
 *         Package:{@link com.dc.gameserver.serverCore.controller.handleController.itemsController}</br>
 *         Comment：秘籍加载请求
 */
@Service
public class spriteLoadingController extends AbstractController {


    @Resource
    private PlayerService playerService;


    @Override
    public void PostConstruct() {
        int ID= spriteLoadingRequest.msgID.ID_VALUE;
        GAME_CONTROLLERS[ID]=this;
        IController.MESSAGE_LITE[ID]=spriteLoadingRequest.getDefaultInstance();
    }

    /**
     * messageLite数据结构体分发
     *
     * @param messageLite 数据载体
     * @param player      active object
     * @throws Exception 注意messageLite应该递交给GC直接处理  ，set null
     */
    @Override
    public void DispatchMessageLit(MessageLite messageLite, PlayerInstance player) throws Exception {
        spriteLoadingRequest loadingRequest= (spriteLoadingRequest) messageLite;
        int type=loadingRequest.getType();   //部位.类型
        int data=loadingRequest.getClientdata();
        loadingRequest=null;
        FastTable<TSprite> sprites=playerService.spriteLoading(player.getID(),type);

        spriteLoadingResponse.Builder builder=spriteLoadingResponse.newBuilder();
        if (sprites!=null){
            for (TSprite sprite:sprites){
                spriteVo.Builder spritevo=spriteVo.newBuilder();
                spritevo.setSpiriteID(sprite.getSpiriteid());
                spritevo.setSpiriteTypeID(sprite.getSpiritetypeid());
                spritevo.setSpiriteItemType(sprite.getItemtype());
                spritevo.setType(sprite.getSpiritetype());
                spritevo.setSpiritePinZhi(sprite.getSpiritepinzhi());
                spritevo.setSpiritePinJie(sprite.getSpiritepinjie());
                spritevo.setSpiriteCurrent(sprite.getSpiritecurrent());
                spritevo.setSpiritelocation(sprite.getSpritelocation());
                builder.addSpriteList(spritevo.build());
            }
            sprites.clear();
            sprites=null;
        }
        builder.setType(type);
        builder.setClientdata(data); //client data
        player.DirectSend(wrappedBufferShort(spriteLoadingResponse.msgID.ID_VALUE,builder.build()));
    }
}
